<?php


namespace App\Model\Basic;


use App\Repository\Basic\CategoryRepository;
use Illuminate\Database\Eloquent\SoftDeletes;

class Documents extends Model
{
    use SoftDeletes;
    protected $table = \App\Utils\Enums\TableEnums::CMS_DOCUMENTS;

    const FIELD_LIST = [
        'name', 'lang', 'title', 'keyword', 'description', 'category_id', 'cover_image', 'fixed_release_time', 'sort', 'status', 'audit', 'admin_user_id',
//        'pid',
        'entity_id', 'entity_name', 'click', 'support', 'comment', 'collect', 'extend',
    ];

    protected $appends = [
        'category_title',
        'status_title',
    ];

    const STATUS_LIST = [
        -1 => '下架',
        0 => '待发布',
//        1 => '1级审核通过',
//        2 => '2级审核通过',
        99 => '已发布'
    ];

    protected $fillable = self::FIELD_LIST;

    public static $searchField = [
        'name' => '标识', // input搜索类型。key 为字段名称，value 为标题
        'title' => '标题', // input搜索类型。key 为字段名称，value 为标题
        'status' => [       // key 为字段名称，value 为相关配置
            'showType' => 'select', // 下拉框选择搜索类型
            'searchType' => '=', // 说明字段在数据库的搜索匹配方式，默认为like查询
            'title' => '状态', // 标题
            'enums' => self::STATUS_LIST,
        ],
        /*'category_id' => [
            'showType' => 'select', // 下拉框选择搜索类型
            'searchType' => '=', // 说明字段在数据库的搜索匹配方式，默认为like查询
            'title' => '状态', // 标题
            'enums' => []
        ],
        'created_at' => [ // key 为字段名称，value 为相关配置
            'showType' => 'created_at', // 日期时间搜索类型
            'title' => '创建时间' // 标题
        ]*/
    ];

    public static function getSearchField($entityId)
    {
        $searchField = self::$searchField;
        return $searchField;
    }

    protected function setFixedReleaseTimeAttribute($value)
    {
        $this->attributes['fixed_release_time'] = empty($value) ? null : $value;
    }

    /**
     * @return bool|mixed|null
     */
    protected function getStatusTitleAttribute()
    {
        return get_value(self::STATUS_LIST, $this->attributes['status'], '');
    }

    /**
     * @return mixed
     */
    protected function getCategoryTitleAttribute()
    {
        return Category::query()->where('id', $this->attributes['category_id'])->value('name');
    }

    protected function setCoverImageAttribute($value)
    {
        $this->attributes['cover_image'] = str_replace(config('app.url'), '', $value);
    }
}